<?php

namespace App\Models;


use App\Exceptions\PhonicsStudentNotFoundException;
use Illuminate\Database\Eloquent\SoftDeletes;

class PhonicsStudent extends BaseModels
{
    use SoftDeletes;

    protected $table = 'phonics_students';
    protected $primaryKey = 'student_id';
    protected $dates = ['deleted_at'];
    public $incrementing = false;

    const SHARE_LIMITS = 5;

    public static function createOrFetch($studentId)
    {
        if ($studentId == 0) {
            throw new PhonicsStudentNotFoundException();
        }
        $student = PhonicsStudent::where('student_id', $studentId)->first();
        if (empty($student)) {
            $student = new PhonicsStudent();
            $student->setStudentId($studentId);
            $student->setSharePrivilege(0);
            $student->save();
        }

        return $student;
    }

    public function getStudentId()
    {
        return $this->student_id;
    }

    public function setStudentId($value)
    {
        $this->student_id = $value;
    }

    public function getSharePrivilege()
    {
        return $this->share_privilege;
    }

    public function setSharePrivilege($value)
    {
        $this->share_privilege = $value;
    }

    public function getShareCount()
    {
        return $this->share_count;
    }

    public function setShareCount($value)
    {
        $this->share_count = $value;
    }

    public function getBonus()
    {
        return floatval($this->bonus);
    }

    public function setBonus($value)
    {
        $this->bonus = $value;
    }

    public function getBonusWithdraw()
    {
        return floatval($this->bonus_withdraw);
    }

    public function setBonusWithdraw($value)
    {
        $this->bonus_withdraw = $value;
    }

    public function getBonusLeft()
    {
        return floatval($this->bonus_left);
    }

    public function setBonusLeft($value)
    {
        $this->bonus_left = $value;
    }

    public function toDisplay()
    {
        return [
            'studentId' => $this->getStudentId(),
            'sharePrivilege' => $this->getSharePrivilege(),
            'bonus' => $this->getBonus(),
            'bonusLeft' => $this->getBonusLeft(),
            'bonusWithdraw' => $this->getBonusWithdraw()
        ];
    }

}